<?php


namespace app\zc\controller;


use app\zc\model\BannerModel;
use cmf\controller\AdminBaseController;

class BannerController extends AdminBaseController
{

    public function index()
    {
        //轮播图列表显示
        $list = BannerModel::order(['list_order'=>'DESC','id'=>'DESC'])
            ->paginate(5);
        $this->assign('list',$list);

        return $this->fetch('banner');

    }
    public function px(){
        $banner = new BannerModel();
        parent::listOrders($banner);
        $this->success('排序成功');
    }

    public function add()
    {
        //添加页面显示
        return $this->fetch();
    }

    public function addPost()
    {
        //图片获取器
        $image = $_POST['image'];
        $image = cmf_get_image_url($image);
        //添加逻辑
        BannerModel::create([
           'url'=>'#',
           'image'=>$image
        ]);
    }

    public function edit(){
        //接收编辑数据id
        $id = input('id','');
        //编辑页面与数据原样显示
        $list = BannerModel::find($id);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function editPost(){
        //接收编辑数据
        $data = input('post.');

        //图片获取器
        $data['image'] = cmf_get_image_url($data['image']);

        $edit = BannerModel::find($data['id']);
        if(!$edit){
            $this->error('未查询到数据');
        }
        $edit->image = $data['image'];
        $edit->url = $data['url'];

        if($edit->save()){
            $this->success('保存成功','banner/index');
        }else{
            $this->error('保存失败');
        }
    }

    public function delete(){
        $id = input('id','');
        if(BannerModel::destroy($id)){
            $this->success('删除成功','banner/index');
        }else{
            $this->error('删除失败');
        }
    }
}